package com.huawei.mpchartdemo1;

import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class BarChartActivity extends AppCompatActivity {

    private BarChart barChart;
    private HorizontalBarChart barChart2;
    private ArrayList<String> xData; //保存X轴坐标数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_chart);
        initView();
//        自定义x轴
        xData = new ArrayList<>();
        xData.add("one");
        xData.add("two");
        xData.add("three");
        xData.add("four");
        xData.add("five");
        xData.add("six");
        XAxis xAxis = barChart.getXAxis();
//        格式化坐标轴数据
        ValueFormatter valueFormatter = new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return xData.get((int) value);
            }
        };
        xAxis.setValueFormatter(valueFormatter);
        xAxis.setDrawLimitLinesBehindData(true);

        List<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(0f, 30f));
        entries.add(new BarEntry(1f, 80f));
        entries.add(new BarEntry(2f, 60f));
        entries.add(new BarEntry(3f, 50f));
        entries.add(new BarEntry(4f, 70f));
        entries.add(new BarEntry(5f, 60f));

//        List<BarEntry> entries2 = new ArrayList<>();
//        entries2.add(new BarEntry(6f, 24f));
//        entries2.add(new BarEntry(7f, 35f));
//        entries2.add(new BarEntry(8f, 84f));
//        entries2.add(new BarEntry(9f, 25f));
//        entries2.add(new BarEntry(10f, 76f));
//        entries2.add(new BarEntry(11f, 34f));
//        BarDataSet set2 = new BarDataSet(entries2, "BarDataSet 2");
//        set2.setColor(Color.RED);
        BarDataSet set = new BarDataSet(entries, "BarDataSet 1");

//        BarData data = new BarData(set, set2);
        BarData data = new BarData(set);
        barChart.setData(data);
        barChart.invalidate();

        XAxis xAxis2 = barChart2.getXAxis();
//        控制 LimitLines 与 actual data 之间的 z-order 。 如果设置为 true，LimitLines 绘制在 actual data 的后面，否则在其前面。 默认值：false
        xAxis2.setDrawLimitLinesBehindData(true);
        BarDataSet set3 = new BarDataSet(entries, "BarDataSet 3");
        BarData data2 = new BarData(set3);
        barChart2.setData(data2);
        barChart2.invalidate();

    }

    private void initView() {
        barChart = (BarChart) findViewById(R.id.barChart);
        barChart2 = (HorizontalBarChart) findViewById(R.id.barChart2);
    }
}